<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/4/28
 * Time: 19:50
 */
namespace app\common\model;

use think\Model;
use think\Db;
use think\Config;

class Mysql extends Model{

    private $db;

    function __construct()
    {
        
//        //$mysqlservername = "rm-bp16q94uh3l21n1ra.mysql.rds.aliyuncs.com:3456";//内网

////$mysqlservername = "rm-bp16q94uh3l21n1ra.mysql.rds.aliyuncs.com:3456";//外网
////        $mysqlusername = "topsydy";
////        $mysqlpassword = "Topsydy5082";
//        $mysqlservername="localhost:3306";
//        $mysqlusername = "root";
//        $mysqlpassword = "root";
//        $DBName = "topsydy";
//        $conn = @mysql_connect($mysqlservername, $mysqlusername, $mysqlpassword);
//        if (!$conn) {
//            die(echoJson(0, "数据库连接失败!"));
//        }
//        mysql_query("SET NAMES UTF8");
//        mysql_query("set character_set_client=utf8");
//        if (mysql_select_db($DBName) === null) {
//            die(echoJson(0, "数据库链接错误!"));
//        }
        Config::load(APP_PATH . 'common/config.php');
        $this->db = Db::connect(Config::get('mysql'));


    }

    /**
     * 查询语句查询是否有值（只查询一行）
     * @param str $sql 查询语句
     */
    function selectSQL($sql, $errmsg = '查询数据出错!') {
        $result = $this->selectSQLAll($sql)[0];
        return $result==NULL?false:$result;
    }


    function count($sql, $errmsg = '查询数据出错!'){
        
    }
    /**
     * 查询语句查询是否有值（只查询一行）
     * @param str $sql 查询语句
     */
    function selectSQLRow($sql, $errmsg = '查询数据出错!') {
        $result = $this->selectSQLAll($sql)[0];
        return $result==NULL?false:$result;
    }

    /**
     * 查询语句查询是否有值（查询所有）
     * @param str $sql 查询语句
     */
    function selectSQLAll($sql, $errmsg = '查询数据出错!') {

        $result = $this->db->query($sql);

        return $result[0]==0?false:$result;
    }
    /**
     * 插入语句
     * @param str $sql 查询语句
     */
    function insertSQL($sql, $errmsg = '插入数据出错!') {
        $result = $this->db->execute($sql);// or die(echoJson(0, $errmsg));

        return $result==0?false:true;
    }

    function insertSQLArray($table,$array){
        //$sql = "insert into"
        return $this->db->table($table)->insert($array);
    }
    /**
     * 删除语句
     * @param str $sql 查询语句
     */
    function deleteSQL($sql, $errmsg = '删除数据出错!') {
        $result = mysql_query($sql) or die(echoJson(0, $errmsg));
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 更新语句
     * @param str $sql 查询语句
     */
    function updateSQL($sql, $errmsg = '更新数据出错!') {
        $result = $this->db->execute($sql);// or die(echoJson(0, $errmsg));

        return $result==0?false:true;
    }

    /**
     * 开始事务
     */
    function beginTransactionSQL($errmsg = '开始事务出错!') {
        mysql_query("BEGIN") or die(echoJson(0, $errmsg));
        mysql_query("SET AUTOCOMMIT=0") or die(echoJson(0, $errmsg));
    }

    /**
     * 提交事务
     */
    function submitTransactionSQL($errmsg = '提交事务出错!') {
        mysql_query("COMMIT") or die(echoJson(0, $errmsg));
        $a = mysql_query("END"); //没有返回值
        mysql_query("SET AUTOCOMMIT=1") or die(echoJson(0, $errmsg));
    }

    /**
     * 滚回事务
     */
    function rollbackTransactionSQL($errmsg = '滚回事务出错!') {
        mysql_query("ROLLBACK") or die(echoJson(0, $errmsg));
        mysql_query("END");
        mysql_query("SET AUTOCOMMIT=1") or die(echoJson(0, $errmsg));
    }
}